
const app = getApp()
Page({
    data: {
        search:{
            filterWord: '',
            rows: 10,
            page: 1,
            tid:null,
        },
        pruduct:[],
        totalResult:'',  // 数量


        show:false  // 显示加载
    },
    onLoad(option) {
        if(option){
          this.onConfirm(option.type);
        }
    },
   // 输入完成
    onConfirm(val){
        console.log("1111111111")
        let search = this.data.search;
        search.filterWord = val;
        search.rows = 10;
        search.page = 1;
        search.tid = null;
        this.setData({
            search,
            pruduct:[] 
        })
        console.log('打印搜索内容', this.data.search)
        this.submit()
    },
   // 跳转
    navigateTo(e){
        app.navigateTo(e)
    },


    // 搜索
    submit(){
        app.product.page(this.data.search)
        .then(res=>{
            console.log(res)
            let totalResult = res.data.totalResult
            this.dataPromise(res.data.list)
            .then(res=>{
                let pruduct = [...this.data.pruduct,...res]
                this.setData({pruduct,totalResult})
            })
        })
    },
     // 转价格
    dataPromise(list){
        return new Promise((relose, reject)=>{
            list.forEach((item) => {
                item.category = JSON.parse(item.category)
                item.colors = JSON.parse(item.colors)
                item.match = JSON.parse(item.match)
                item.params = JSON.parse(item.params)
                item.slideshows = JSON.parse(item.slideshows)
                this.getminPrice(item.params)
                    .then(res => {
                        item.price = res
                    })
            })
            relose(list)
        })
    },

    getminPrice(params){
        return new Promise((resole,reject)=>{
            let prices=[]
            for (let i = 0;i<params.length;i++){
                for (let t = 0; t < params[i].term_list.length; t++) {
                    for (let s = 0; s < params[i].term_list[t].specifications.length; s++) {
                        let price = (params[i].term_list[t].specifications[s].money / params[i].term_list[t].value / 100).toFixed(2)
                        prices.push(price)
                    }
                }
            }
            let minprice = Math.min.apply(null, prices);
            resole(minprice)
        })
    },  

    // 下拉刷新
    onPullDownRefresh() {
        let e={detail:{value:''}}
        this.onConfirm(e)
        setTimeout(() => {
            my.stopPullDownRefresh({
                complete(res) {
                }
            })
        }, 1000)
    },
    stopPullDownRefresh() {
        my.stopPullDownRefresh({
            complete(res) {
                // console.log(res, new Date())
            }
        })
    },
    //   下拉加载
    onReachBottom() {
        let search = this.data.search,
        pruduct = this.data.pruduct,
        totalResult = this.data.totalResult;
        try {
            // 判断是否还有数据需要加载
            if (pruduct.length < totalResult) {
                search.page++
                this.setData({search})
                this.submit();
            }
        } catch (e) {}
    },
});
